﻿using BLL;
using System;
using System.Data;
using System.Drawing;
using System.Linq;

public partial class ManagementPlatform_SignatureManage_PicTure : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string COMPANYID = Session["COMPANYID"].ToString();

        string[] month = new string[12] { "1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月" };

        string sql = string.Format(@" SELECT f.Pro_ID 项目编号,f.fatherid,CASE when d.m_id=0
                then (SELECT fd.pro_name FROM qs_fundplan fd
                 Where fd.Pro_ID = f.Pro_ID)
                  else (SELECT fm.PROJECTNAME FROM qs_fundmonplan fm Where fm.id = d.m_id) end 项目名称,
                    m.n_company_id 公司编号,c.CMY_NAME 公司名称,
               d.endlastfund 截止上月资金计划, d.endlastsumfund 截止上月累计资金,
                d.nowfund  本月资金计划,d.m_id,m.m_month
                FROM qs_fundplandetail d,qs_monthplan m,qs_fundplan f,Sys_Company c
                Where d.mainid=m.fundid and d.n_fundid=f.Pro_ID and m.n_company_id=c.CMY_ID
                and m.issave=1   and fatherid=0
              and m.n_company_id in(" + COMPANYID + @")
        ");

        // DataTable dt = new OracleHelper().GetDataTable(sql);
        DataTable dt = new BaseBLL().Query(sql);

        string year = DateTime.Now.Year.ToString();

        float[] d = new float[12];

        d[0] = float.Parse(dt.AsEnumerable().Where(s => s.Field<string>("m_month").ToString() == year + "01").Sum(s => float.Parse(s.Field<decimal>("本月资金计划").ToString())).ToString());
        d[1] = float.Parse(dt.AsEnumerable().Where(s => s.Field<string>("m_month").ToString() == year + "02").Sum(s => float.Parse(s.Field<decimal>("本月资金计划").ToString())).ToString());
        d[2] = float.Parse(dt.AsEnumerable().Where(s => s.Field<string>("m_month").ToString() == year + "03").Sum(s => float.Parse(s.Field<decimal>("本月资金计划").ToString())).ToString());
        d[3] = float.Parse(dt.AsEnumerable().Where(s => s.Field<string>("m_month").ToString() == year + "04").Sum(s => float.Parse(s.Field<decimal>("本月资金计划").ToString())).ToString());
        d[4] = float.Parse(dt.AsEnumerable().Where(s => s.Field<string>("m_month").ToString() == year + "05").Sum(s => float.Parse(s.Field<decimal>("本月资金计划").ToString())).ToString());
        d[5] = float.Parse(dt.AsEnumerable().Where(s => s.Field<string>("m_month").ToString() == year + "06").Sum(s => float.Parse(s.Field<decimal>("本月资金计划").ToString())).ToString());
        d[6] = float.Parse(dt.AsEnumerable().Where(s => s.Field<string>("m_month").ToString() == year + "07").Sum(s => float.Parse(s.Field<decimal>("本月资金计划").ToString())).ToString());
        d[7] = float.Parse(dt.AsEnumerable().Where(s => s.Field<string>("m_month").ToString() == year + "08").Sum(s => float.Parse(s.Field<decimal>("本月资金计划").ToString())).ToString());
        d[8] = float.Parse(dt.AsEnumerable().Where(s => s.Field<string>("m_month").ToString() == year + "09").Sum(s => float.Parse(s.Field<decimal>("本月资金计划").ToString())).ToString());
        d[9] = float.Parse(dt.AsEnumerable().Where(s => s.Field<string>("m_month").ToString() == year + "10").Sum(s => float.Parse(s.Field<decimal>("本月资金计划").ToString())).ToString());
        d[10] = float.Parse(dt.AsEnumerable().Where(s => s.Field<string>("m_month").ToString() == year + "11").Sum(s => float.Parse(s.Field<decimal>("本月资金计划").ToString())).ToString());
        d[11] = float.Parse(dt.AsEnumerable().Where(s => s.Field<string>("m_month").ToString() == year + "12").Sum(s => float.Parse(s.Field<decimal>("本月资金计划").ToString())).ToString());

        float[] sj = new float[12] { 12, 10, 5, 8, 20, 4, 32, 1, 16, 7, 10, 11 };

        //画图初始化
        Bitmap bMap = new Bitmap(500, 600);
        Graphics gph = Graphics.FromImage(bMap);
        gph.Clear(Color.White);
        PointF cPt = new PointF(50, 420);//中心点
        PointF[] xPt = new PointF[3] { new PointF(cPt.Y + 15, cPt.Y), new PointF(cPt.Y, cPt.Y - 8), new PointF(cPt.Y, cPt.Y + 8) };//X轴三角形
        PointF[] yPt = new PointF[3] { new PointF(cPt.X, cPt.X - 15), new PointF(cPt.X - 8, cPt.X), new PointF(cPt.X + 8, cPt.X) };//Y轴三角形
        gph.DrawString("公司月度资金折线图", new Font("宋体", 14), Brushes.Black, new PointF(cPt.X + 60, cPt.X));//图表标题
        //画X轴
        gph.DrawLine(Pens.Black, cPt.X, cPt.Y, cPt.Y, cPt.Y);
        gph.DrawPolygon(Pens.Black, xPt);
        gph.FillPolygon(new SolidBrush(Color.Black), xPt);
        gph.DrawString("月份", new Font("宋体", 12), Brushes.Black, new PointF(cPt.Y + 20, cPt.Y + 10));
        //画Y轴
        gph.DrawLine(Pens.Black, cPt.X, cPt.Y, cPt.X, cPt.X);
        gph.DrawPolygon(Pens.Black, yPt);
        gph.FillPolygon(new SolidBrush(Color.Black), yPt);
        gph.DrawString("单位(万元)", new Font("宋体", 12), Brushes.Black, new PointF(0, 7));

        float max = d.Max(); //最大值

        for (int i = 1; i <= 12; i++)
        {
            //画Y轴刻度
            if (max < 700 && max >= 0)
            {
                if (i < 11)
                {
                    gph.DrawString((i * 100).ToString(), new Font("宋体", 11), Brushes.Black, new PointF(cPt.X - 30, cPt.Y - i * 30 - 6));
                    gph.DrawLine(Pens.Black, cPt.X - 3, cPt.Y - i * 30, cPt.X, cPt.Y - i * 30);
                }
            }

            //  gph.DrawString((i * 100).ToString(), new Font("宋体", 11), Brushes.Black, new PointF(cPt.X - 30, cPt.Y - i * 30 - 6));
            //  gph.DrawLine(Pens.Black, cPt.X - 3, cPt.Y - i * 30, cPt.X, cPt.Y - i * 30);

            //画X轴项目
            gph.DrawString(month[i - 1], new Font("宋体", 11), Brushes.Black, new PointF(cPt.X + i * 30 - 5, cPt.Y + 5));

            #region 本年的计划资金

            //画点
            gph.DrawEllipse(Pens.Black, cPt.X + i * 30 - 1.5F, cPt.Y - d[i - 1] * 3 - 1.5F, 3, 3);
            gph.FillEllipse(new SolidBrush(Color.Black), cPt.X + i * 30 - 1.5F, cPt.Y - d[i - 1] * 3 - 1.5F, 3, 3);
            //画数值
            gph.DrawString(d[i - 1].ToString(), new Font("宋体", 11), Brushes.Black, new PointF(cPt.X + i * 30, cPt.Y - d[i - 1] * 3 - 15));
            //画折线
            if (i > 1) gph.DrawLine(Pens.Red, cPt.X + (i - 1) * 30, cPt.Y - d[i - 2] * 3, cPt.X + i * 30, cPt.Y - d[i - 1] * 3);

            #endregion 本年的计划资金

            #region 实际金额

            gph.DrawEllipse(Pens.Black, cPt.X + i * 30 - 1.5F, cPt.Y - d[i - 1] * 3 - 1.5F, 3, 3);
            gph.FillEllipse(new SolidBrush(Color.Black), cPt.X + i * 30 - 1.5F, cPt.Y - sj[i - 1] * 3 - 1.5F, 3, 3);
            //画数值
            gph.DrawString(sj[i - 1].ToString(), new Font("宋体", 11), Brushes.Black, new PointF(cPt.X + i * 30, cPt.Y - sj[i - 1] * 3 - 15));
            //画折线
            if (i > 1) gph.DrawLine(Pens.Blue, cPt.X + (i - 1) * 30, cPt.Y - sj[i - 2] * 3, cPt.X + i * 30, cPt.Y - sj[i - 1] * 3);

            #endregion 实际金额
        }

        //绘制标识
        Font font = new Font("Arial", 10, FontStyle.Regular); //绘制范围框
        gph.DrawRectangle(new Pen(Brushes.Blue), 130, 450, 200, 40);   //绘制小矩形

        gph.FillRectangle(Brushes.Red, 170, 455, 20, 10);
        gph.DrawString("计划金额", font, Brushes.Red, 200, 455);

        gph.FillRectangle(Brushes.Blue, 170, 475, 20, 10);
        gph.DrawString("实际金额", font, Brushes.Blue, 200, 475);

        System.IO.MemoryStream ms = new System.IO.MemoryStream();
        bMap.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
        Response.BinaryWrite(ms.GetBuffer());
    }
}